﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DTheme
{
    /// <summary>
    /// 按照步骤 1a 或 1b 操作，然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
    ///
    /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:DTheme"
    ///
    ///
    /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:DTheme;assembly=DTheme"
    ///
    /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用，
    /// 并重新生成以避免编译错误:
    ///
    ///     在解决方案资源管理器中右击目标项目，然后依次单击
    ///     “添加引用”->“项目”->[浏览查找并选择此项目]
    ///
    ///
    /// 步骤 2)
    /// 继续操作并在 XAML 文件中使用控件。
    ///
    ///     <MyNamespace:DExpanderForList/>
    ///
    /// </summary>
    public class DExpanderForList : Expander
    {
        static DExpanderForList()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DExpanderForList), new FrameworkPropertyMetadata(typeof(DExpanderForList)));
        }
        public DExpanderForList()
        {
            this.Loaded += DExpanderForList_Loaded;
        }

        private void DExpanderForList_Loaded(object sender, RoutedEventArgs e)
        {
            DListBox dlist;
            UIElement p = VisualTreeHelper.GetParent(this) as UIElement;
            for (int i = 0; i < 15; i++)
            {
                if (p == null) return;

                dlist = p as DListBox;
                if (dlist == null)
                {
                    p = VisualTreeHelper.GetParent(p) as UIElement;
                }
                else
                {
                    if (dlist.IsCheckStyle)
                    {
                        headCheckBox = null;
                        contentChecBboxList.Clear();
                        findCheckBox_InHead();
                        findCheckBox_InContent();
                        updateHead();
                    }
                    return;
                }
            }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            ContentControl c = GetTemplateChild("head") as ContentControl;
            if (c != null)
            {
                c.MouseDoubleClick += changeIsExpanded;
            }
        }
        private void changeIsExpanded(object sender, MouseButtonEventArgs e)
        {
            this.IsExpanded = !IsExpanded;
        }

        DCheckBoxForList headCheckBox;
        List<DCheckBoxForList> contentChecBboxList = new List<DCheckBoxForList>();
        private void findCheckBox_InHead()
        {
            UIElement headchild = this.Header as UIElement;
            for (int i = 0; i < 20; i++)
            {
                headCheckBox = headchild as DCheckBoxForList;
                if (headCheckBox != null) break;
                if (VisualTreeHelper.GetChildrenCount(headchild) == 0) break;
                headchild = VisualTreeHelper.GetChild(headchild, 0) as UIElement;
            }
        }
        private void findCheckBox_InContent()
        {
            findCheckBox_ite(this.Content as UIElement);
        }
        private void findCheckBox_ite(UIElement start)
        {
            if(start == null)
            {
            }else
            {
                DCheckBoxForList c = start as DCheckBoxForList;
                if (c == null)
                {
                    for (int ii = 0; ii < VisualTreeHelper.GetChildrenCount(start); ii++)
                    {
                        findCheckBox_ite(VisualTreeHelper.GetChild(start, ii) as UIElement);
                    }
                }
                else
                {
                    contentChecBboxList.Add(c);
                }
            }
        }

        public void updateHead()
        {
            //updateCheckBoxSource();
            if (headCheckBox  == null || contentChecBboxList.Count == 0) return;
            int checkCount = 0;
            foreach (DCheckBoxForList c in contentChecBboxList)
            {
                if (c.IsChecked == true)
                {
                    checkCount++;
                }
            }
            if (checkCount == 0)
            {
                headCheckBox.IsChecked = false;
            }else if(checkCount == contentChecBboxList.Count)
            {
                headCheckBox.IsChecked = true;
            }else
            {
                headCheckBox.IsChecked = null;
            }
         
        }
        public void updateContent()
        {
            //updateCheckBoxSource();
            if (headCheckBox == null || contentChecBboxList.Count == 0) return;        
            if(headCheckBox.IsChecked != null)
            {
                foreach (DCheckBoxForList c in contentChecBboxList)
                {
                    c.IsChecked = headCheckBox.IsChecked;
                }
            }
        }

    }
}
